import java.sql.Connection;
import java.sql.DriverManager;

public class Mysql {
    private String user,//用户名
            password,//密码
            host,//主机
            port,//端口
            dataBase,//数据库名
            connectUrl,
            characterEncoding = "UTF-8",//字符编码
            mysqlDriver = "com.mysql.cj.jdbc.Driver";//驱动名
    private Connection connection = null;

    //无参构造方法(使用默认值)
    Mysql() {
        user = "root";
        password = "123456";
        host = "localhost";
        port = "3306";
        dataBase = "mysql";
        showConnectionMessage();
    }
//带参构造方法(用户名，密码，主机，接口，数据库名)
    Mysql(String connectUser,
          String connectPassword,
          String connectHost,
          String connectPort,
          String dataBaseName) {
        user = connectUser;
        password = connectPassword;
        host = connectHost;
        port = connectPort;
        dataBase = dataBaseName;
        showConnectionMessage();
    }

    void showConnectionMessage() {
        System.out.println("--------------------");
        System.out.println("user:" + user);
        System.out.println("password:" + password);
        System.out.println("host:" + host);
        System.out.println("port:" + port);
        System.out.println("dataBaseName:" + dataBase);
        System.out.println("characterEncoding:" + characterEncoding);
        System.out.println("driver:" + mysqlDriver);
        System.out.println("connectUrl:" + getConnectUrl());
        System.out.println("--------------------");
    }

    void loadMysqlDriver(String driverName) {
        try {
            System.out.println("loading……");
            Class.forName(driverName);
            System.out.println("driver loaded successfully.");
        } catch (Exception e) {
            System.out.println(e);
        }
        System.out.println("--------------------");
    }

    String getConnectUrl() {
        connectUrl = "jdbc:mysql://" + host + ":" + port + "/" + dataBase + "?" + "characterEncoding=" + characterEncoding;
        return connectUrl;
    }

    Connection connect() {
        loadMysqlDriver(mysqlDriver);
        connectUrl = getConnectUrl();
        try {
            System.out.println("connecting……");
            connection = DriverManager.getConnection(connectUrl, user, password);
            System.out.println("the connection is successful.");
        } catch (Exception e) {
            System.out.println(e);
        }
        System.out.println("--------------------");
        return connection;
    }

    void setUser(String newUser) {
        System.out.println("oldUser:" + user);
        user = newUser;
        System.out.println("newUser:" + user);
    }

    void setPassword(String newPassword) {
        System.out.println("oldPassword:" + password);
        password = newPassword;
        System.out.println("newPassword:" + password);
    }

    void setHost(String newHost) {
        System.out.println("oldHost:" + host);
        host = newHost;
        System.out.println("newHost:" + host);
    }

    void setPort(String newPort) {
        System.out.println("oldPort:" + port);
        port = newPort;
        System.out.println("newPort:" + port);
    }

    void setDataBase(String newDataBase) {
        System.out.println("oldCharacterEncoding:" + newDataBase);
        dataBase = newDataBase;
        System.out.println("newCharacterEncoding:" + newDataBase);
    }

    void setCharacterEncoding(String newCharacterEncoding) {
        System.out.println("oldCharacterEncoding:" + newCharacterEncoding);
        characterEncoding = newCharacterEncoding;
        System.out.println("newCharacterEncoding:" + newCharacterEncoding);
    }

    void setMysqlDriver(String newMysqlDriver) {
        System.out.println("oldHost:" + mysqlDriver);
        mysqlDriver = newMysqlDriver;
        System.out.println("newHost:" + mysqlDriver);
    }

    String getUser() {
        return user;
    }

    String getPassword() {
        return password;
    }

    String getHost() {
        return host;
    }

    String getPort() {
        return password;
    }

    String getDataBase() {
        return dataBase;
    }

    String getCharacterEncoding() {
        return characterEncoding;
    }

    String getMysqlDriver() {
        return mysqlDriver;
    }
}
